<!--
/// 
/// MaDAson - Toolkit for Model Driven Software Development
///
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 
-->
<%@ Page Language="C#" %>
<%@ Import Namespace="Madason.MetaModel" %>
<%@ Import Namespace="Madason.MetaModel.DAO" %>


CREATE OR REPLACE PROCEDURE MDATEST IS
/*Deklaration der globale Variablen*/
	v_counter_table number;
	v_counter_column number;
BEGIN
 <%	
	string tmpTblName = ""; 
	foreach (Entity cls in Model.DeepGetClasses().Filter(Stereotype.Entity)) { %>

<%	if(cls.Stereotype == Stereotype.Entity) { %>

SELECT	COUNT(object_id) INTO v_counter_table  FROM user_objects
WHERE object_name ='<%= cls.TableNameClear %>' AND object_type ='TABLE';
			IF v_counter_table=0 THEN 
				BEGIN --1
					EXECUTE IMMEDIATE' 
					CREATE TABLE "<%= cls.TableNameClear %>"
					(
						<%
						System.Collections.Generic.List<string> defs = new System.Collections.Generic.List<string>();
						%>
						<% foreach(PersistedAttribute attr in AllAttributes(cls)) { %>
						<%	if (attr.Stereotype == Stereotype.Transient) continue; %>
						<%	defs.Add(attr.ColumnNameOracleDDL + " " + attr.ColumnType + (attr.Stereotype == Stereotype.PrimaryKey ?"NOT NULL ENABLE,\n\t\t\t\t\t\tCONSTRAINT \"PK_"+ cls.TableNameClear+"\" PRIMARY KEY ("+attr.ColumnNameOracleDDL+")" : "")); %>	
						<% } %>	
	<!--
						<% foreach (PersistedAssociation assoc in cls.ActiveAssociations) { %>
							<% 
								defs.Add(assoc.RoleNameOracleDDL + " NUMBER(*,0)");
		
							}%>
					-->
	<!--<% foreach (PersistedAssociation assoc in cls.ActiveAssociations) { %>
	<% 
		defs.Add(assoc.ColumnNameOracleDDL + " NUMBER(*,0)");
		}%>-->
	<!--<% foreach (StateMachine sm in cls.StateMachines) { %>
	<%	defs.Add(sm.Name + " NUMBER(*,0)"); %>
	<% } %>-->
						<% for (int i=0; i<defs.Count; i++) { %>
							<%= "\t\t\t\t\t"+defs[i] + ((i<defs.Count-1) ? ",\n\t" : "") %>
						<% } %>
					)';
				<%	}	%>


				EXECUTE IMMEDIATE'CREATE SEQUENCE "Seq_<%= cls.TableNameClear %>" START WITH 1 NOMAXVALUE INCREMENT BY 1 NOCYCLE';
			END;
		END IF; --1
<% } %>

<%foreach (Entity cls in Model.DeepGetClasses().Filter(Stereotype.Entity)) { %>
<%	if (cls.Stereotype == Stereotype.Entity) { %>


<%foreach(PersistedAssociation assoc in cls.ActiveAssociations) {  %>
SELECT COUNT(column_id) INTO v_counter_column FROM
USER_TAB_COLUMNS WHERE table_name ='<%= cls.TableNameClear %>'
AND column_name='<%= (assoc.TargetClass.FindOneByStereotype(Stereotype.PrimaryKey) as PersistedAttribute).ColumnNameClear%>';
 
	IF v_counter_column =0 THEN 
		BEGIN --2

			EXECUTE IMMEDIATE'
			ALTER TABLE  "<%= cls.TableNameClear %>" add (<%= (assoc.TargetClass.FindOneByStereotype(Stereotype.PrimaryKey) as PersistedAttribute).ColumnNameOracleDDL%>    <%= assoc.ColumnType%>)
			';
		END;
	END IF;--2


SELECT COUNT(*) INTO v_counter_column FROM user_CONSTRAINTS
WHERE table_name='<%= cls.TableNameClear %>'
AND constraint_type = 'R'
AND constraint_name='FK_<%= cls.TableNameClear %>_<%= (assoc.TargetClass as Entity).TableNameClear %>';

	IF v_counter_column = 0 THEN 
		BEGIN--3
			EXECUTE IMMEDIATE'
			ALTER TABLE "<%= cls.TableNameClear %>" ADD CONSTRAINT "FK_<%= cls.TableNameClear %>_<%= (assoc.TargetClass as Entity).TableNameClear %>"
			FOREIGN KEY (<%= (assoc.TargetClass.FindOneByStereotype(Stereotype.PrimaryKey) as PersistedAttribute).ColumnNameOracleDDL%>) 
			REFERENCES "<%= (assoc.TargetClass as Entity).TableNameClear%>" (<%= (assoc.TargetClass.FindOneByStereotype(Stereotype.PrimaryKey) as PersistedAttribute).ColumnNameOracleDDL%>)
			';
		END;
	END IF;--3

<%	} %>
<%
foreach(PersistedAssociation assoc in cls.ComplexAssociations) {  
%>
<%
if (cls.TableNameClear!=tmpTblName){
%>
--Generate DDL for ComplexAssociations(n:n Relationship).
SELECT COUNT(object_id) INTO v_counter_table  FROM user_objects
WHERE object_name ='<%= cls.TableNameClear %>_<%= (assoc.TargetClass as Entity).TableNameClear%>' AND object_type ='TABLE';
	IF v_counter_table=0 THEN --IF STUFE 0
		BEGIN--4
		EXECUTE IMMEDIATE' 
		CREATE TABLE  "<%= cls.TableNameClear %>_<%= (assoc.TargetClass as Entity).TableNameClear%>"
		(
			<%= cls.PrimaryKey.ColumnNameOracleDDL%>   <%= assoc.ColumnType%> ,
			<%= (assoc.TargetClass.FindOneByStereotype(Stereotype.PrimaryKey) as PersistedAttribute).ColumnNameOracleDDL%>    <%= assoc.ColumnType %> 
			<!--CONSTRAINT  PRIMARY KEY ("<%=cls.PrimaryKey.ColumnNameClear %>", "<%= (assoc.TargetClass.FindOneByStereotype(Stereotype.PrimaryKey) as PersistedAttribute).ColumnNameClear%>")-->
		)';
		EXECUTE IMMEDIATE' 
		ALTER TABLE "<%= cls.TableNameClear %>_<%= (assoc.TargetClass as Entity).TableNameClear%>" ADD (  CONSTRAINT "PK_<%= cls.TableNameClear %>_<%= (assoc.TargetClass as Entity).TableNameClear%>" PRIMARY KEY ("<%=cls.PrimaryKey.ColumnNameClear %>", "<%= (assoc.TargetClass.FindOneByStereotype(Stereotype.PrimaryKey) as PersistedAttribute).ColumnNameClear%>")  ENABLE  VALIDATE  )
		';				
		END;
	END IF;--4
<%tmpTblName= (assoc.TargetClass as Entity).TableNameClear; }%>
		<% } %>
	<%	} %>
<% } %>
END;
<script runat="server">
	private Model @model;
	
	public AttributeList AllAttributes(Entity cls)
	{
		AttributeList list = new AttributeList();
		
		if (cls.SuperClass != null) list.Add(cls.PrimaryKey);
		list.AddRange(cls.Attributes);
		return list;
	}

	
	public Model Model
	{
		get { return model; }
		set { model = value; }
	}

	public Madason.MetaModel.Attribute GetPrimaryKey(Entity cls)
	{
		return cls.PrimaryKey;
	}	
	
</script>